package com.baseframe.auth.utils;

import com.baseframe.auth.entity.BaseUserDetails;
import com.baseframe.system.entity.BasePermission;
import com.baseframe.tool.container.Kv;
import com.baseframe.user.entity.BaseUser;
import org.springframework.security.core.authority.AuthorityUtils;

import java.util.List;
import java.util.stream.Collectors;

public class WrapperUtil {
    public static BaseUserDetails buildUserDetails(BaseUser user, List<BasePermission> permissions, Kv map){

        if (user==null){
            return null;
        }

        String authorityStr = "";

        authorityStr = permissions.stream().map(BasePermission::getPermission).collect(Collectors.joining(","));

        return new BaseUserDetails(
                user.getId(),
                user.getUsername(),
                user.getPassword(),
                user.getRoleId(),
                map,
                AuthorityUtils.commaSeparatedStringToAuthorityList(authorityStr)
        );
    }
}
